<template>
    <el-dialog
      :title="isUpdate?'修改违规事件':'添加违规事件'"
      :visible="visible"
      width="460px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @update:visible="updateVisible"
      :close-on-click-modal="false">
        <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
            <el-form-item label="日期:" prop="record_date">
                <el-date-picker
                    style="width: 100%"
                    v-model="form.record_date"
                    placeholder="选择日期"
                    type="date"
                    value-format="yyyy-MM-dd"
                    label-format="yyyy-MM-dd"
                    :picker-options="pickerOptions"
                />
            </el-form-item>
            <el-form-item label="一级推广员:" prop="promote_1_id">
                <PageSelect
                    :placeholder="'请选择一级推广员'"
                    v-model="form.promote_1_id"
                    :whereName="'realname'"
                    :url="'/userpromote/index?level=1'"
                    :optionLable="'realname'"
                    :optionvalue="'id'"
                    :multiple="false"
                />
            </el-form-item>
            <el-form-item label="推广员:" prop="promote_id">
                <PageSelect
                    :placeholder="'请选择推广员'"
                    v-model="form.promote_id"
                    :whereName="'realname'"
                    :url="'/userpromote/index'"
                    :optionLable="'realname'"
                    :optionvalue="'id'"
                    :multiple="false"
                />
            </el-form-item>
            <el-form-item label="游戏包:" prop="package_id">
                <PageSelect
                    :placeholder="'请选择游戏包'"
                    v-model="form.package_id"
                    :whereName="'package_name'"
                    :url="'/Package/getChannelPackage'"
                    :optionLable="'package_name'"
                    :optionvalue="'id'"
                    :multiple="false"
                />
            </el-form-item>
            <el-form-item label="角色ID:" prop="role_id">
                <el-input
                    :placeholder="'请输入角色ID'"
                    v-model="form.role_id"></el-input>
            </el-form-item>
            <el-form-item label="区服角色:" prop="server_role">
                <el-input
                    :placeholder="'请输入区服角色'"
                    v-model="form.server_role"></el-input>
            </el-form-item>
            <el-form-item label="交易查询:" prop="result">
                <el-input
                    :placeholder="'请输入交易查询'"
                    v-model="form.result"></el-input>
            </el-form-item>
            <el-form-item label="归属:" prop="ascription">
                <el-input
                    :placeholder="'请输入归属'"
                    v-model="form.ascription"></el-input>
            </el-form-item>
            <el-form-item label="结算类型" prop="type">
                <el-radio-group v-model="form.type">
                    <el-radio :label="0">非日结</el-radio>
                    <el-radio :label="1">日结</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="结算状态" prop="jies_status" v-if="isUpdate">
                <el-radio-group v-model="form.jies_status">
                    <el-radio :label="0">未结算</el-radio>
                    <el-radio :label="1">已结算</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="updateVisible(false)">取消</el-button>
            <el-button
                type="primary"
                @click="save"
                :loading="loading">保存
            </el-button>
        </div>
    </el-dialog>
</template>  
<script>
import PageSelect from "@/components/pageSelect/index.vue";
export default {
    components:{PageSelect},
    props: {
        // 弹窗是否打开
        visible: Boolean,
        // 修改回显的数据
        data: Object
    },
    data(){
        return{
            // 表单数据
            form: Object.assign({}, this.data),
            // 表单验证规则
            rules: {
                record_date:[
                {required: true, message: '请选择日期', trigger: 'blur'}
                ],
                promote_1_id: [
                {required: true, message: '请选择推广员', trigger: 'blur'}
                ],
                promote_id: [
                {required: true, message: '请选择推广员', trigger: 'blur'}
                ],
                package_id: [
                {required: true, message: '请选择游戏包', trigger: 'blur'}
                ],
                role_id: [
                {required: true, message: '请输入角色ID', trigger: 'blur'}
                ],
                server_role: [
                {required: true, message: '请输入区服角色', trigger: 'blur'}
                ],
                result: [
                {required: true, message: '请输入交易查询', trigger: 'blur'}
                ],
                ascription: [
                {required: true, message: '请输入归属', trigger: 'blur'}
                ],
                type: [
                {required: true, message: '请选择结算类型', trigger: 'blur'}
                ],
                jies_status: [
                {required: true, message: '请选择结算状态', trigger: 'blur'}
                ],
            },
            // 提交状态
            loading: false,
            // 是否是修改
            isUpdate: false,
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > (Date.now()-24*60*60*1000) - 8.64e6
                }
            }, 
        }
    },
    watch: {
        data() {
            if (this.data) {
                this.form = Object.assign({}, this.data);
                this.isUpdate = true;
            } else {
                this.form = {type: 0};
                this.isUpdate = false;
            }
        }
    },
    methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/Settlementviolation/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  },
}
</script>
<style scoped>
</style>
